package com.primb.bench.project.home.model;

import com.primb.bench.common.constant.AppConstant;
import com.primb.bench.common.module.http.service.ApiClient;
import com.primb.bench.common.module.http.service.ApiService;
import com.primb.bench.project.approval.entity.NextPersonEntity;
import com.primb.bench.project.home.contract.LoanCalculateContract;
import com.primb.bench.project.home.entity.ApplyNextUserEntity;
import com.primb.bench.project.home.entity.HttpApplyResultEntity;
import com.primb.bench.project.home.entity.HttpCheckProEntity;
import com.primb.bench.project.home.entity.HttpLogoEntity;
import com.primb.bench.project.home.entity.HttpResultEntity;
import com.primb.bench.project.home.entity.HttpStrEntity;
import com.primb.bench.project.home.entity.LoanCalculateInfoEntity;
import com.primb.netlibs.converter.RxSchedulerUtils;
import com.primb.netlibs.entity.HttpResultResponse;
import com.primb.netlibs.exception.ApiException;

import java.util.HashMap;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by Administrator on 2017/05/05
 */

public class LoanCalculateModelImpl implements LoanCalculateContract.Model {

    @Override
    public Observable<HttpLogoEntity> getLogoInfo() {
        return ApiClient.initService(ApiService.class).getLoanLogoInfo()
                .compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    @Override
    public Observable<HttpResultEntity.DataBean> getLoanCalculate(LoanCalculateInfoEntity infoEntity) {
        HashMap<String, String> map = new HashMap<>();
        /**
         "priceId"---定价流水号
         "priceScene"---定价场景
         "agmId"---协议号
         "priceType"---试算01，定价02
         "org"---所在机构
         "date"---测算日期
         "custId"---客户号
         "custIdM"---母公司客户号
         "custName"---客户名称
         "custType"---客户类型
         "custCreditLvl"---客户信用等级
         "custScale"---客户规模
         "nowSumGp"---累积毛利润
         "nowSumGx"---累积贡献点
         "nowUseGx"---已使用贡献点
         "nowRemainGx"---剩余贡献点
         "isScore"---是否使用贡献点
         "isScoreM"---是否使用母公司贡献点
         "nowUseGxM"---母公司已使用贡献点
         "nowRemainGxM"---母公司剩余贡献点
         "adjustPd"---违约概率调整
         "prdtId"---业务品种
         "lnAmt"---贷款金额
         "lnCur"---贷款币种
         "rePayWay----还款方式
         "lnTerm"---贷款期限
         "termUtil"---期限单位
         "lnIndu"---贷款投向
         "lnIntType"---利率类型
         "lnAssType"---主担保方式
         "detailAssType"---具体担保方式
         "marginAmt"---保证金金额
         "finPleAmt"---金融质押品金额
         "accRec"---应收账款金额
         "comLivReaEstAmt"---商用或居住用房地产金额
         "othMortAmt"---其他抵质押品金额
         "addValueTax"---增值税及附加
         "lnIncomeTax"---所得税
         */


        //定价信息
        map.put("priceId", infoEntity.getLogoInfoEntity().getLogoNumber());//
        map.put("priceType", infoEntity.getLogoInfoEntity().getLogoPriceType());
        map.put("priceScene", infoEntity.getLogoInfoEntity().getLogoProtocolType());//
        map.put("agmId", infoEntity.getLogoInfoEntity().getLogoProtocolNum());//
        map.put("org", AppConstant.USER_PART_ID);//orgId
        map.put("date", infoEntity.getLogoInfoEntity().getLogoDate().replace("-", ""));//
        //客户信息
        map.put("custMngId", AppConstant.USER_NAME_ID);//
        map.put("custMngName", AppConstant.USER_NAME);//
        map.put("custId", infoEntity.getClientInfoEntity().getCustomerNum());//
        map.put("custIdM", infoEntity.getClientInfoEntity().getCustomerNumM());//
        map.put("custName", infoEntity.getClientInfoEntity().getCustomerName());//
        map.put("custType", infoEntity.getClientInfoEntity().getCustomerTypeId());//
        map.put("custCreditLvl", infoEntity.getClientInfoEntity().getCustomerCreditId());//
        map.put("custScale", infoEntity.getClientInfoEntity().getCustomerSizeId());//
        map.put("nowSumGp", infoEntity.getClientInfoEntity().getCustomerAllProfit());//
        map.put("nowSumGx", infoEntity.getClientInfoEntity().getCustomerAllContribution());//
        map.put("nowUseGx", infoEntity.getClientInfoEntity().getCustomerUsedContribution());//
        map.put("nowRemainGx", infoEntity.getClientInfoEntity().getCustomerContributionNum());//
        map.put("isScore", infoEntity.getClientInfoEntity().getCustomerIsContribution());//
        /**
         * 是否使用母公司贡献点
         */
        map.put("isScoreM", infoEntity.getClientInfoEntity().getCustomerIsContributionM());//
//        map.put("nowSumGpM", "0");
//        map.put("nowSumGxM", "0");
        map.put("nowUseGxM", infoEntity.getClientInfoEntity().getCustomerUsedContributionM());//
        map.put("nowRemainGxM", infoEntity.getClientInfoEntity().getCustomerContributionNumM());//
        map.put("adjustPd", infoEntity.getClientInfoEntity().getCustomerAdjustRate());//
        //贷款信息
        map.put("prdtId", infoEntity.getLoanInfoEntity().getLoanBusinessTypeId());//
        map.put("lnAmt", infoEntity.getLoanInfoEntity().getLoanMoney());//
        map.put("lnCur", infoEntity.getLoanInfoEntity().getLoanTypeId());//
        map.put("lnTerm", infoEntity.getLoanInfoEntity().getLoanDate());//
        if (infoEntity.getLoanInfoEntity().getLoanDateUnit().equals("月")) {
            map.put("termUtil", "1");//
        } else {
            map.put("termUtil", "0");//
        }
        map.put("rePayWay", infoEntity.getLoanInfoEntity().getLoanRepaymentId());//
        map.put("lnIndu", infoEntity.getLoanInfoEntity().getLoanIndustryId());//
        map.put("lnIntType", infoEntity.getLoanInfoEntity().getLoanRateTypeId());//
//        map.put("lnIntType", "L08001");
        //担保
        map.put("lnAssType", infoEntity.getEnsureInfoEntity().getEnsureMainTypeId());//
        map.put("detailAssType", infoEntity.getEnsureInfoEntity().getEnsureConcretenessTypeId());//
        map.put("marginAmt", "0");//
        map.put("finPleAmt", infoEntity.getEnsureInfoEntity().getLnFinImpawnAmt());//
        map.put("accRec", infoEntity.getEnsureInfoEntity().getLnAcctRecvAmt());//
        map.put("comLivReaEstAmt", infoEntity.getEnsureInfoEntity().getLnPropertyAmt());//
        map.put("othMortAmt", infoEntity.getEnsureInfoEntity().getLnOthMortAmt());//
        map.put("highGuarantee", infoEntity.getEnsureInfoEntity().getHighGuarantee());//
        map.put("commonGuarantee", infoEntity.getEnsureInfoEntity().getCommonGuarantee());//
        //税收
        map.put("addValueTax", infoEntity.getTaxInfoEntity().getTaxVat());//
        map.put("lnIncomeTax", infoEntity.getTaxInfoEntity().getTaxIncome());//


        HashMap<String, String> mapPro = new HashMap<>();
        mapPro.put("start", "0");
        mapPro.put("limit", "100");

        mapPro.put("custId", infoEntity.getClientInfoEntity().getCustomerNum());
//        Observable<HttpCheckProEntity> checkPromise = ApiClient.initService(ApiService.class).checkPromise(infoEntity.getClientInfoEntity().getCustomerNum());
       return ApiClient.initService(ApiService.class).getLoanCalculateResult(map)
                .flatMap(new Func1<HttpResultEntity, Observable<HttpResultEntity.DataBean>>() {
                    @Override
                    public Observable<HttpResultEntity.DataBean> call(HttpResultEntity resultEntity) {
                        if (!resultEntity.isSuccess()) {
                            Observable.error(new ApiException("请求数据失败，请检查网络"));
                        }
                        return Observable.just(resultEntity.getData());
                    }
                }).compose(RxSchedulerUtils.normalSchedulersTransformer());

//        return Observable.merge(loanCalculateResult, checkPromise).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    @Override
    public Observable<HttpStrEntity> deletePromiseInfo(String priceId) {
        return ApiClient.initService(ApiService.class).deletePromiseInfo(priceId)
                .compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    @Override
    public Observable<ApplyNextUserEntity> getCalculateApplyRate(LoanCalculateInfoEntity infoEntity) {
        HashMap<String, String> map = new HashMap<>();

        /**
         "priceId"---定价流水号
         "priceScene"---定价场景
         "agmId"---协议号
         "priceType"---试算01，定价02
         "org"---所在机构
         "date"---测算日期
         "custId"---客户号
         "custIdM"---母公司客户号
         "custName"---客户名称
         "custType"---客户类型
         "custCreditLvl"---客户信用等级
         "custScale"---客户规模
         "nowSumGp"---累积毛利润
         "nowSumGx"---累积贡献点
         "nowUseGx"---已使用贡献点
         "nowRemainGx"---剩余贡献点
         "isScore"---是否使用贡献点
         "isScoreM"---是否使用母公司贡献点
         "nowUseGxM"---母公司已使用贡献点
         "nowRemainGxM"---母公司剩余贡献点
         "adjustPd"---违约概率调整
         "prdtId"---业务品种
         "lnAmt"---贷款金额
         "lnCur"---贷款币种
         "lnTerm"---贷款期限
         "termUtil"---期限单位
         "lnIndu"---贷款投向
         "lnIntType"---利率类型
         "lnAssType"---主担保方式
         "detailAssType"---具体担保方式
         "marginAmt"---保证金金额
         "finPleAmt"---金融质押品金额
         "accRec"---应收账款金额
         "comLivReaEstAmt"---商用或居住用房地产金额
         "othMortAmt"---其他抵质押品金额
         "addValueTax"---增值税及附加
         "lnIncomeTax"---所得税
         */


        //定价信息
        map.put("priceId", infoEntity.getLogoInfoEntity().getLogoNumber());//
        map.put("priceType", infoEntity.getLogoInfoEntity().getLogoPriceType());
        map.put("priceScene", infoEntity.getLogoInfoEntity().getLogoProtocolType());//
        map.put("agmId", infoEntity.getLogoInfoEntity().getLogoProtocolNum());//
        map.put("org", AppConstant.USER_PART_ID);//orgId
        map.put("date", infoEntity.getLogoInfoEntity().getLogoDate().replace("-", ""));//
        map.put("applyCustMngName",infoEntity.getLogoInfoEntity().getLogoManagerNum());
        map.put("applyOrg",infoEntity.getLogoInfoEntity().getLogoPartId());
        //客户信息
        map.put("custMngId", AppConstant.USER_NAME_ID);//
        map.put("custMngName", AppConstant.USER_NAME);//
        map.put("custId", infoEntity.getClientInfoEntity().getCustomerNum());//
        map.put("custIdM", infoEntity.getClientInfoEntity().getCustomerNumM());//
        map.put("custName", infoEntity.getClientInfoEntity().getCustomerName());//
        map.put("custType", infoEntity.getClientInfoEntity().getCustomerTypeId());//
        map.put("custCreditLvl", infoEntity.getClientInfoEntity().getCustomerCreditId());//
        map.put("custScale", infoEntity.getClientInfoEntity().getCustomerSizeId());//
        map.put("nowSumGp", infoEntity.getClientInfoEntity().getCustomerAllProfit());//
        map.put("nowSumGx", infoEntity.getClientInfoEntity().getCustomerAllContribution());//
        map.put("nowUseGx", infoEntity.getClientInfoEntity().getCustomerUsedContribution());//
        map.put("nowRemainGx", infoEntity.getClientInfoEntity().getCustomerContributionNum());//
        map.put("isScore", infoEntity.getClientInfoEntity().getCustomerIsContribution());//
        /**
         * 是否使用母公司贡献点
         */
        map.put("isScoreM", infoEntity.getClientInfoEntity().getCustomerIsContributionM());//
//        map.put("nowSumGpM", "0");
//        map.put("nowSumGxM", "0");
        map.put("nowUseGxM", infoEntity.getClientInfoEntity().getCustomerUsedContributionM());//
        map.put("nowRemainGxM", infoEntity.getClientInfoEntity().getCustomerContributionNumM());//
        map.put("adjustPd", infoEntity.getClientInfoEntity().getCustomerAdjustRate());//
        //贷款信息
        map.put("prdtId", infoEntity.getLoanInfoEntity().getLoanBusinessTypeId());//
        map.put("lnAmt", infoEntity.getLoanInfoEntity().getLoanMoney());//
        map.put("lnCur", infoEntity.getLoanInfoEntity().getLoanTypeId());//
        map.put("lnTerm", infoEntity.getLoanInfoEntity().getLoanDate());//
        if (infoEntity.getLoanInfoEntity().getLoanDateUnit().equals("月")) {
            map.put("termUtil", "1");//
        } else {
            map.put("termUtil", "0");//
        }
        map.put("rePayWay", infoEntity.getLoanInfoEntity().getLoanRepaymentId());//
        map.put("lnIndu", infoEntity.getLoanInfoEntity().getLoanIndustryId());//
        map.put("lnIntType", infoEntity.getLoanInfoEntity().getLoanRateTypeId());//
        //担保
        map.put("lnAssType", infoEntity.getEnsureInfoEntity().getEnsureMainTypeId());//
        map.put("detailAssType", infoEntity.getEnsureInfoEntity().getEnsureConcretenessTypeId());//
        map.put("marginAmt", "0");//
        map.put("finPleAmt", infoEntity.getEnsureInfoEntity().getLnFinImpawnAmt());//
        map.put("accRec", infoEntity.getEnsureInfoEntity().getLnAcctRecvAmt());//
        map.put("comLivReaEstAmt", infoEntity.getEnsureInfoEntity().getLnPropertyAmt());//
        map.put("othMortAmt", infoEntity.getEnsureInfoEntity().getLnOthMortAmt());//
        map.put("highGuarantee", infoEntity.getEnsureInfoEntity().getHighGuarantee());//
        map.put("commonGuarantee", infoEntity.getEnsureInfoEntity().getCommonGuarantee());//
        //税收
        map.put("addValueTax", infoEntity.getTaxInfoEntity().getTaxVat());//
        map.put("lnIncomeTax", infoEntity.getTaxInfoEntity().getTaxIncome());//
        //测算结果
        map.put("lnCostBdIr", infoEntity.getResultInfoEntity().getLnpCostBdRate());//
        map.put("lnCostIr", infoEntity.getResultInfoEntity().getLnpCostRate());//
        map.put("lnpTargetRate", infoEntity.getResultInfoEntity().getLnpTargetRate());//
        map.put("custGdIr", infoEntity.getResultInfoEntity().getLnpCustRate());//
        map.put("lnpMarketIr", infoEntity.getResultInfoEntity().getLnpMarketIr());//
        map.put("paraFtpRate", infoEntity.getResultInfoEntity().getLncFtpRate());//
        map.put("paraPrdFeeRate", infoEntity.getResultInfoEntity().getLncFeeRate());//
        map.put("paraPrdFeeBDRate", infoEntity.getResultInfoEntity().getLncFeeBdRate());//
        map.put("paraElRate", infoEntity.getResultInfoEntity().getElRate());//
        map.put("paraPdRate", infoEntity.getResultInfoEntity().getLncPd());//
        map.put("paraLgdRate", infoEntity.getResultInfoEntity().getLncLgd());//
        map.put("paraCapCostRate", infoEntity.getResultInfoEntity().getCapCostRate());//
        map.put("paraCapOcuRate", infoEntity.getResultInfoEntity().getLncEcPara());//
        map.put("paraCapRetRate", infoEntity.getResultInfoEntity().getLncLowCapRet());//
        map.put("paraVatRate", infoEntity.getResultInfoEntity().getLncVat());//
        map.put("paraIncomeTax", infoEntity.getResultInfoEntity().getLnIncomeTax());//
        map.put("paraPrdTarRet", infoEntity.getResultInfoEntity().getLncPrdTarRet());//
        map.put("paraCustScoreValue", infoEntity.getResultInfoEntity().getLnDiscountValue());//
        map.put("paraScoreUse", infoEntity.getResultInfoEntity().getLncScoreUse());//
        map.put("paraScoreleft", infoEntity.getResultInfoEntity().getLncScoreleft());//
        //承诺
        map.put("promiseAdjRate", infoEntity.getPromiseApplyEntity().getPromiseAdjustRate());//
        map.put("adjLnpCustRate", infoEntity.getPromiseApplyEntity().getPromiseAdjustDirectRate());//

        if (infoEntity.getPromiseApplyEntity().isPromiseDetail()) {
            map.put("sceneBgnDate", infoEntity.getPromiseApplyEntity().getPromiseStartDate().replace("-", ""));//
            map.put("sceneEndDate", infoEntity.getPromiseApplyEntity().getPromiseEndDate().replace("-", ""));//
            map.put("nowDpMonAvg", infoEntity.getPromiseApplyEntity().getPromiseDepositDayNow());//
            map.put("sceneCurDpAvg", infoEntity.getPromiseApplyEntity().getPromiseDepositDay());//
            map.put("nowDpMonBal", infoEntity.getPromiseApplyEntity().getPromiseDepositSum());//
            map.put("sceneCurDpBal", infoEntity.getPromiseApplyEntity().getPromiseDepositSumNow());//
            map.put("promBaseAcc", infoEntity.getPromiseApplyEntity().isPromiseBasicAccount() ? "1" : "0");//
            map.put("promForCurAcc", infoEntity.getPromiseApplyEntity().isPromiseSettleAccount() ? "1" : "0");//
            map.put("promPayAcc", infoEntity.getPromiseApplyEntity().isPromiseSalaryAccount() ? "1" : "0");//
            map.put("othPromScene", infoEntity.getPromiseApplyEntity().getPromiseOtherSceneDetail());//
            map.put("othPromPoint", infoEntity.getPromiseApplyEntity().getPromiseOtherScene());//
            map.put("remark", infoEntity.getPromiseApplyEntity().getPromiseRemark());//
            map.put("sceneIr", infoEntity.getPromiseApplyEntity().getPromiseFavorableRate());//
            map.put("overUsePoint", infoEntity.getPromiseApplyEntity().getPromisePoint());//
        }
        map.put("applyRate", infoEntity.getPromiseApplyEntity().getApplyRate());//
        map.put("applyDate", infoEntity.getPromiseApplyEntity().getApplyDate());//
        map.put("applyLowPriceReason", infoEntity.getPromiseApplyEntity().getApplyReason());//

        return ApiClient.initService(ApiService.class).getCalculateApplyRate(map).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    @Override
    public Observable<HttpStrEntity> getApplyRateNoProcess(LoanCalculateInfoEntity infoEntity) {
        HashMap<String, String> map = new HashMap<>();
        map.put("priceId", infoEntity.getLogoInfoEntity().getLogoNumber());//
        map.put("custId", infoEntity.getClientInfoEntity().getCustomerNum());//
        map.put("orgId", AppConstant.USER_PART_ID);//orgId
        map.put("promiseAdjRate", infoEntity.getPromiseApplyEntity().getPromiseAdjustRate());//
        map.put("adjLnpCustRate", infoEntity.getPromiseApplyEntity().getPromiseAdjustDirectRate());//
        map.put("applyRate", infoEntity.getPromiseApplyEntity().getApplyRate());//
        map.put("applyDate", infoEntity.getPromiseApplyEntity().getApplyDate());//
        map.put("applyLowPriceReason", infoEntity.getPromiseApplyEntity().getApplyReason());//
        map.put("adjLnpCustRate", infoEntity.getPromiseApplyEntity().getPromiseAdjustDirectRate());//
        return ApiClient.initService(ApiService.class).getApplyRateNoProcess(map).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    @Override
    public Observable<HttpApplyResultEntity> submitNextPerson(LoanCalculateInfoEntity infoEntity, NextPersonEntity bean) {
        HashMap<String, String> map = new HashMap<>();
        if (bean.getUserId() != null) {
            map.put("nextUsers", bean.getUserId());
        } else {
            map.put("nextUsers", "");
        }
        if (bean.getUserName() != null) {
            map.put("nextUserNames", bean.getUserName());
        } else {
            map.put("nextUserNames", "");
        }

        if (bean.getPhone() != null) {
            map.put("nextUserPhones", bean.getPhone());
        } else {
            map.put("nextUserPhones", "");
        }
        map.put("applyRate1", infoEntity.getPromiseApplyEntity().getApplyRate());//
        map.put("applyDate", infoEntity.getPromiseApplyEntity().getApplyDate());//
        map.put("applyLowPriceReason", infoEntity.getPromiseApplyEntity().getApplyReason());//
        map.put("priceId", infoEntity.getLogoInfoEntity().getLogoNumber());//
        map.put("sceneIr", infoEntity.getPromiseApplyEntity().getPromiseFavorableRate());//
        return ApiClient.initService(ApiService.class).submitNextPerson(map).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    @Override
    public Observable<HttpStrEntity> saveResult(LoanCalculateInfoEntity infoEntity) {
        HashMap<String, String> map = new HashMap<>();
        map.put("priceId", infoEntity.getLogoInfoEntity().getLogoNumber());//
        map.put("sceneIr", infoEntity.getPromiseApplyEntity().getPromiseFavorableRate());//
        map.put("promiseAdjRate", infoEntity.getPromiseApplyEntity().getPromiseAdjustRate());//
        map.put("adjLnpCustRate", infoEntity.getPromiseApplyEntity().getPromiseAdjustDirectRate());//
        map.put("overUsePoint", infoEntity.getPromiseApplyEntity().getPromisePoint());//
        return ApiClient.initService(ApiService.class).saveLoanCalculateResult(map)
                .compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    @Override
    public Observable<HttpCheckProEntity> checkPromise(String custId) {
        Observable<HttpCheckProEntity> checkPromise = ApiClient.initService(ApiService.class).checkPromise(custId);
        return checkPromise.compose(RxSchedulerUtils.normalSchedulersTransformer());
    }
}